<template>
  <div>
    <!-- 列表 -->
    <el-table :data="searchList" style="width: 100%">
      <el-table-column label="产品名称" width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="产品地址">
        <template slot-scope="scope">
          <el-tag size="medium">{{ scope.row.address }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="产品描述">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.desc }}</span>
        </template>
      </el-table-column>
      <el-table-column>
        <template #header>
          <el-input v-model="searchValue" placeholder="请输入搜索内容"></el-input>
        </template>
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹框 -->

    <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="产品名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="产品地址" :label-width="formLabelWidth">
          <el-input v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="产品描述" :label-width="formLabelWidth">
          <el-input v-model="form.desc" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleModify"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 辅助函数
import { mapState, mapActions, mapMutations } from "vuex";
export default {
  name: "List",
  data() {
    return {
      dialogFormVisible: false,
      form: {
        name: "",
        desc: "",
        address: "",
        id: '',
      },
      formLabelWidth: "120px",
      searchValue: '',
    };
  },
  computed: {
    // 批量数据
    ...mapState(["list", "a", "b", "c"]),
    searchList() {
      return this.$store.getters.searchList(this.searchValue)
    }
  },
  methods: {
    // 批量方法
    ...mapActions(["Fetch_List"]),
    ...mapMutations(["Delete_List", "Edit_List"]),
    // 打开弹框
    handleEdit(index, row) {
      this.form = { ...row }
      this.dialogFormVisible = true;
    },
    // 删除
    handleDelete(index, row) {
      // 确认框
      this.$confirm("确认要删除吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          // 提示框
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          // 删除函数
          this.Delete_List(row.id);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 确定修改
    handleModify() {
      // this.$store.commit('Edit_List', this.form)
      this.Edit_List(this.form)
      this.dialogFormVisible = false
    }
  },
  created() {
    this.Fetch_List();
  }
};
</script>